import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/config/ColorConfig.dart';

import 'FFGestureDetector.dart';


class FFInk extends StatefulWidget {
  FFGestureDetector child;
  Color? splashColor; // 水波颜色
  Color? highlightColor; // 高亮颜色
  String? notes; //注释
  bool? disabled; //禁用效果
  
  FFInk({
    Key? key,
    required this.child,
    this.notes,
    this.disabled = false,
    this.splashColor,
    this.highlightColor = Colors.transparent,
  }) : super(key: key);

  @override
  State<FFInk> createState() => _FFInkState();
}

class _FFInkState extends State<FFInk> {
  Function? _onClick;
  Function? _onLongPress;
  
  
  @override
  Widget build(BuildContext context) {
    if (widget.disabled == true) {
      //失效了，这儿控制一下，直接点击不了了
      widget.child.onClick = null;
      widget.child.onLongPress = null;
      return widget.child;
    }

    _onClick = widget.child.onClick;
    _onLongPress = widget.child.onLongPress;

    widget.splashColor ??= ColorConfig().splashColor;

    BoxDecoration? boxDecoration;
    BorderRadius? borderRadius;
    if (widget.child.decoration != null && widget.child.decoration is BoxDecoration) {
      boxDecoration = widget.child.decoration as BoxDecoration;

      if (boxDecoration != null && boxDecoration.borderRadius is BorderRadius) {
        borderRadius = boxDecoration.borderRadius as BorderRadius;
      }
    }

    double? top = 0.0, bottom = 0.0, left = 0.0, right = 0.0;
    if (widget.child.margin != null && widget.child.margin is EdgeInsets) {
      EdgeInsets insets = widget.child.margin as EdgeInsets;
      top = insets.top;
      bottom = insets.bottom;
      left = insets.left;
      right = insets.right;
    }

    widget.child.color = null;
    widget.child.decoration = null;
    widget.child.onClick = null;
    widget.child.onLongPress = null;
    widget.child.margin = null;

    return Material(
      color: Colors.transparent,
      child: Container(
        margin: EdgeInsets.only(left: left,right: right,top: top,bottom: bottom),
        child: Ink(
          //设置背景
          decoration: boxDecoration,
          child: InkResponse(
            borderRadius: borderRadius,
            //点击或者toch控件高亮时显示的控件在控件上层,水波纹下层
            highlightColor: widget.highlightColor,
            //点击或者toch控件高亮的shape形状
            highlightShape: BoxShape.rectangle,
            //.InkResponse内部的radius这个需要注意的是，我们需要半径大于控件的宽，如果radius过小，显示的水波纹就是一个很小的圆，
            //水波纹的半径
            radius: max(  2 * (widget.child.width?? 300.0 ),2 * (widget.child.heigth ?? 300.0 )),
            //水波纹的颜色
            splashColor: widget.splashColor,
            //true表示要剪裁水波纹响应的界面 false不剪裁 如果控件是圆角不剪裁的话水波纹是矩形
            containedInkWell: true,
            //点击事件
            onTap: () {
              repeatedClickJudgment(_onClick);
            },
            onLongPress: () {
              repeatedClickJudgment(_onLongPress);
            },
            child: widget.child,
          ),
        ),
      ),
    );

  }

  ///重复点击判断
  repeatedClickJudgment(Function? fun){
    if (fun != null) {

      if (isBeyondClickInterval(widget.child)) {
        fun();
      }
    }
  }

  int? lastMill;//上一次点击时间
  bool isBeyondClickInterval(FFGestureDetector wid){
    int currentMill = DateTime.now().millisecondsSinceEpoch;
    if (lastMill == null) {
      lastMill = currentMill;
      return true;
    }

    int interval = currentMill - (lastMill ?? 0);
    if (interval >= (wid.minClickIntervalMill ?? 600)) {
      lastMill = currentMill;
      return true;
    }

    print("最少时间间隔${wid.minClickIntervalMill}不满足");
    return false;
  }
}

